//
//  SY_Setting_NewPassWordViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 16/8/18.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Setting_NewPassWordViewController.h"

@interface SY_Setting_NewPassWordViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,UITextFieldDelegate>
{
    UIButton *_nextBtn;
    
    UILabel  *titleLabel;
    
    UITextField *_newPassWordTextField;
    UITextField *_makeSurePassWordTextField;
    
}

@property (nonatomic, strong) UITableView *myTable;

@end

@implementation SY_Setting_NewPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navBar.nav_titleLabel.text = @"设置新密码";
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view setBackgroundColor:RGBCOLOR(240,239,245)];
    
    [self.view addSubview:self.myTable];
    
    _nextBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_nextBtn setFrame:CGRectMake(12, 156 + SafeAreaTopHeight, SCREENWIDTH - 24, 38)];
    _nextBtn.layer.cornerRadius = 5;
    [_nextBtn setTitle:@"完成" forState:(UIControlStateNormal)];
    [_nextBtn setTintColor:[UIColor whiteColor]];
    [_nextBtn setBackgroundColor:DEFAULTCOLOR];
    [_nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    _nextBtn.enabled = NO;
//    _nextBtn.alpha = 0.5;
    [self.view addSubview:_nextBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)nextBtnAction:(UIButton *)sender {
    
    NSString *newStr = [_newPassWordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *makeSureStr = [_makeSurePassWordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    
    if (_newPassWordTextField.text.length<6 ||_newPassWordTextField.text.length>16) {
        [Utils alertViewWithMessage:@"请将密码限制在6-16位以内"];
        return;
    }
    
    if (_makeSurePassWordTextField.text.length<6 || _makeSurePassWordTextField.text.length>16) {
        
        [Utils alertViewWithMessage:@"请将确认密码限制在6-16位以内"];
        return;
    }
    
    //判断是否含有标点
    NSCharacterSet *set = [NSCharacterSet punctuationCharacterSet];
    NSRange range = [_makeSurePassWordTextField.text rangeOfCharacterFromSet:set];
    if (range.location != NSNotFound) {
        
        [Utils alertViewWithMessage:@"密码中不能含有标点符号"];
        return;
    }
    
    if (![_newPassWordTextField.text isEqualToString:_makeSurePassWordTextField.text]) {
        
        [Utils alertViewWithMessage:@"两次输入的密码不一致"];
        return;
        
    }
    
    if (newStr.length > 0 && makeSureStr.length > 0) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        [dic setObject:@"api_user_repass" forKey:@"Method"];
        [dic setObject:[Utils MD5EncryptWith:newStr] forKey:@"newpass"];
        [dic setObject:USERID forKey:@"userid"];
        [dic setObject:[SYAccountTool ShareAccount].account.mobile forKey:@"mobile"];
        [dic setObject:self.verificationCodeStr forKey:@"verify"];
        
        [Utils get:nil parameters:dic success:^(NSInteger recode, id jsonObject) {
            
            NSLog(@"%@",jsonObject);
            NSDictionary *dict=(NSDictionary *)jsonObject;
            NSString *meg=[dict objectForKey:@"re_info"];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:meg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            
        } failure:^(NSInteger recode, NSError *error) {
            
        }];
        
        
    }else {
     
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请填写完整信息" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    });
    
}

#pragma mark - TabelViewDataSource & UITabelViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    
    if (cell == nil) {
        cell  = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"identifier"];
        
    }
    
    
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 44)];
    
    titleLabel.tag = 51;
    [titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    [cell.contentView addSubview:titleLabel];
    
    if (indexPath.row == 0) {
        
        titleLabel.text = @"密码";
        
        _newPassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(107, 0, 200, 44)];
        [_newPassWordTextField setFont:[UIFont systemFontOfSize:16]];
        [_newPassWordTextField setTextColor:RGBCOLOR(59, 59, 59)];
        _newPassWordTextField.placeholder = @"6-16位数字、字母组成";
        _newPassWordTextField.secureTextEntry = YES;
        [_newPassWordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        _newPassWordTextField.delegate = self;
        [cell.contentView addSubview:_newPassWordTextField];
        //[self displayPhoneNumberMark:YES];
        
        
    }else if (indexPath.row == 1) {
        
        titleLabel.text = @"确认密码";
        
        _makeSurePassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(107, 0, 200, 44)];
        [_makeSurePassWordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        _makeSurePassWordTextField.delegate = self;
        _makeSurePassWordTextField.secureTextEntry = YES;
        _makeSurePassWordTextField.placeholder = @"再次输入密码";
        [_makeSurePassWordTextField setFont:[UIFont systemFontOfSize:16]];
        [_makeSurePassWordTextField setTextColor:RGBCOLOR(59, 59, 59)];
        //_makeSurePassWordTextField.keyboardType = UIKeyboardTypeNumberPad;
        [cell.contentView addSubview:_makeSurePassWordTextField];
        
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(15, 43.5, SCREENWIDTH, 0.5)];
    [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
    [cell.contentView addSubview:lineView];
    
    if (indexPath.row == 1) {
        lineView.x = 0;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    
   if (textField==_newPassWordTextField)
    {
        if (_newPassWordTextField.text.length > 16) {
            UITextRange *markedRange = [_newPassWordTextField markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [_newPassWordTextField.text rangeOfComposedCharacterSequenceAtIndex:16];
            _newPassWordTextField.text = [_newPassWordTextField.text substringToIndex:range.location];
        }
        
    }else if (textField==_makeSurePassWordTextField)
    {
        if (_makeSurePassWordTextField.text.length > 16) {
            UITextRange *markedRange = [_makeSurePassWordTextField markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [_makeSurePassWordTextField.text rangeOfComposedCharacterSequenceAtIndex:16];
            _makeSurePassWordTextField.text = [_makeSurePassWordTextField.text substringToIndex:range.location];
        }
        
    }
}
#pragma mark - Get

- (UITableView *)myTable {
    if (_myTable == nil) {
        _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, 88) style:(UITableViewStylePlain)];
        _myTable.delegate = self;
        _myTable.dataSource = self;
        _myTable.rowHeight = 44;
        _myTable.scrollEnabled = NO;
        _myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _myTable;
}

@end
